<?php
namespace App\Http\Controllers\ApiUser;

use App\Service\Member\ChatService;
use Illuminate\Http\Request;

class ChatController extends BaseController
{
    //聊天室

    public function __construct(ChatService $service)
    {
        $this->db = $service;
    }

    //列表
    public function getList(Request $request)
    {
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 10);
        $res = $this->db->getList($pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //创建聊天室
    public function add(Request $request)
    {
        $token = $request->post('token', '');
        $pas['title'] = $request->post('title', '');
        $pas['thumb'] = $request->post('thumb', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        if (!$pas['title']) {
            return $this->toJson(-2, '昵称必须的');
        }
        $res = $this->db->add($pas, $uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //内容列表
    public function cntList(Request $request)
    {
        $token = $request->post('token', '');
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 10);
        $id = $request->post('chat_id', 0);
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $res = $this->db->cntList($id, $uid, $pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //发送内容
    public function send(Request $request)
    {
        $token = $request->post('token', '');
        $pas['chat_id'] = $request->post('chat_id', 0);
        $pas['type'] = $request->post('type', 0);
        $pas['cnt'] = $request->post('cnt', '');
        $pas['uid'] = $this->db->uidByToken($token);
        if (!$pas['uid']) {
            return $this->toJson(-2, '没有登录');
        }
        if (!$pas['chat_id']) {
            return $this->toJson(-2, '聊天室必须的');
        }
        if (!$pas['type'] || !$pas['cnt']) {
            return $this->toJson(-2, '类型或内容必须的');
        }
        $res = $this->db->send($pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //表情包
    public function emojiList(Request $request)
    {
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 10);
        $res = $this->db->emojiList($pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }
}